﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using System.Linq;
using System.Text;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using QFTMCO;
using ReflectionStudio.Core.Helpers;
using INTEX.DataBase;
using INTEX.DataBase.Tools;
namespace WPFExcelReport
{

    public partial class QFTMCOData : BaseFunction
    { 
        INTEXDBC INTEXDBC;
        public QFTMCOData()
            : base()
        {

        }
        public QFTMCOData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
            INTEXDBC = new INTEXDBC(OracleDB, proxy);
        }
        public QFTMCOData(OracleConnection _myConnection, IntexReptServBLClient _proxy, string userID = "        ")
            : base(_myConnection, _proxy, userID)
        {
            INTEXDBC = new INTEXDBC(OracleDB, proxy);
        }
        public DataSet LoadSetYarnPrice()
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                string filePath = Path.Combine(PathHelper.ApplicationPath, @"Documents\QFTYARNPRICE.XML");
                if (System.IO.File.Exists(filePath))
                {
                    setResult.ReadXml(filePath);
                    setResult.Tables[0].TableName = "YARNPRICE";
                    setReturn.Merge(setResult);
                }
                else
                {
                    setResult = LoadYarnPrice();
                    setReturn.Merge(setResult);
                    SaveYarnPrice(setReturn.Tables["YARNPRICE"]);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public void SaveYarnPrice(DataTable table)
        {
            DataSet SetReturn = new DataSet();
            try
            {
                SetReturn.Merge(table);
                string filePath = Path.Combine(PathHelper.ApplicationPath, @"Documents\QFTYARNPRICE.XML");
                if (System.IO.File.Exists(filePath))
                {
                    System.IO.File.Delete(filePath);
                }
                SetReturn.WriteXml(filePath);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public DataSet LoadSetProdCost()
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                string filePath = Path.Combine(PathHelper.ApplicationPath, @"Documents\PRODCOST.XML");
                if (System.IO.File.Exists(filePath))
                {
                    setResult.ReadXml(filePath);
                    setResult.Tables[0].TableName = "PRODCOST";
                }
                setReturn.Merge(setResult);
                filePath = Path.Combine(PathHelper.ApplicationPath, @"Documents\PROCESSING.XML");
                setResult = new DataSet();
                if (System.IO.File.Exists(filePath))
                {
                    setResult.ReadXml(filePath);
                    setResult.Tables[0].TableName = "PROCESSING";
                }
                setReturn.Merge(setResult);

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet ReSetSETUP(string company, string user)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataSet settemp = new DataSet();
            try
            {
                ArrayList tables = new ArrayList();
                string fileroot = PathHelper.ApplicationPath + @"\Documents\";
                string filePath = fileroot + "QFTQUOTSETUP.xls";
                if (System.IO.File.Exists(filePath))
                {
                    if (exAgent == null)
                    {
                        exAgent = new ExcelX.ExcelLib.ExcelAgent(filePath);
                    }
                    setResult = exAgent.LoadDataTableDef(filePath, "");
                    foreach (DataTable tbTemp in setResult.Tables)
                    {
                        string tbName = tbTemp.TableName;
                        settemp = new DataSet();
                        settemp.DataSetName = "QFTQUOTSETUP";
                        settemp.Merge(tbTemp);
                        filePath = fileroot + tbName + ".XML";
                        if (System.IO.File.Exists(filePath))
                        {
                            System.IO.File.Delete(filePath);
                        }
                        settemp.WriteXml(filePath);
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

            return setReturn;
        }
        public void SaveSetProdCost(DataTable table)
        {
            DataSet SetReturn = new DataSet();

            try
            {
                SetReturn.Merge(table);
                string filePath = Path.Combine(PathHelper.ApplicationPath, "QFTPRODCOST.XML");
                if (System.IO.File.Exists(filePath))
                {
                    System.IO.File.Delete(filePath);
                }
                SetReturn.WriteXml(filePath);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public DataSet LoadYarnPrice()
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT ROWNUM AS YID,'0' AS YSUPID,'HLT' AS YSUPNAME,GWBART AS YYARN,GWBVAR AS YVAR,GWBFARBE AS YCOLOR"
                    + ", GWPRVH*0.01 AS YPPRICE,GWPRAK*0.01 AS YCPRICE,GWPRKU*0.01 AS YFPRICE,'KG' AS YUOM, GWAENDDAT AS YEFFECTDATE,"
                    + "GWNEUBEN AS YUSER, GWNEUDAT AS YDATE,"
                    + "'' AS YREMARK1,'' AS YREMARK2,'' AS  YREMARK3"
                    + " FROM GWERT";
                setResult = Connect2Proxy(or1);
                if (setResult.Tables.Count > 0)
                {
                    setResult.Tables[0].TableName = "YARNPRICE";
                }
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet LoadWAFARBEN(DataRow dwInput)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT DISTINCT WAFART,WAFVAR,WAFZUART,WAFZUSATZ,WAFCOLORIT,WAFSTATUS,WAFSFKZ,WAFBEZ"
               + " FROM INTEX.WAFARBEN WHERE  WAFART = :WAFART AND WAFVAR=:WAFVAR order by WAFZUSATZ desc";
                or1.Parameters.Add("WAFART", OracleType.NVarChar).Value = dwInput["WAART"].ToString();
                or1.Parameters.Add("WAFVAR", OracleType.NVarChar).Value = dwInput["WAVAR"].ToString();
                setResult = Connect2Proxy(or1);
                if (setResult.Tables.Count > 0)
                {
                    setResult.Tables[0].TableName = "WAFARBEN";
                }
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet LoadWAART(string UsingID, string article = "")
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            OracleCommand or1;
            try
            {
                UsingID = UsingID.Trim();
                UsingID = UsingID.Replace("\t", "");
                UsingID = Left(UsingID + "        ", 8);
                or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT DISTINCT WAART, WAVAR"
               + " FROM INTEX.WASTAMM WHERE  rownum<200 and WAKUNDNR='00000000' AND WAART like '" + article + "%' AND warfertkz = 'E' AND WAFFERTKZ = 'E' "
                + " AND (WANEUB = '" + UsingID + "' OR '" + UsingID + "' IN ('FPDE    ','XHJUN   ')) order by 1 desc";
                setResult = Connect2Proxy(or1);
                if (setResult.Tables.Count > 0)
                {
                    setResult.Tables[0].TableName = "WAART";
                }
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet GetData(DataRow dwInput)
        {
            DataSet Return = new DataSet();
            DataSet Result = new DataSet();
            DataRow row = null;
            try
            {
                string mandant=dwInput["VKKMANDANT"].ToString();
                string KRS = dwInput["VKKAUFKRS"].ToString();
                string LFD = dwInput["VKKAUFLFD"].ToString();
                Result=INTEXDBC.QueryVKKOPF(dwInput, 1);
                Return.Merge(Result);
                row = SETINTEXDB.VKPOS.NewRow();
                row["VKPMANDANT"] = mandant;
                row["VKPAUFKRS"] = KRS;
                row["VKPAUFLFD"] = LFD;
                Result = INTEXDBC.QueryVKPOS(row, 2);
                Return.Merge(Result);
                foreach (DataRow rvp in Return.Tables["VKPOS"].Rows)
                {
                    row = SETINTEXDB.WDECK.NewRow();
                    row["WDMANDANT"] = rvp["VKPMANDANT"];
                    row["WDAUFKRS"] = rvp["VKPAUFKRS"];
                    row["WDAUFLFD"] = rvp["VKPAUFLFD"];
                    row["WDAUFPOS"] = rvp["VKPAUFPOS"];
                    Result = INTEXDBC.QueryWDECK(row, 3);
                    Return.Merge(Result);
                }
                string disponumber=KRS+LFD;
                row=SETINTEXDB.ABKOPF.NewRow();
                row["ABKMANDANT"]=MANDANT;
                row["ABKKDAUFTNR"]=disponumber;
                Result=INTEXDBC.QueryABKOPF(row,3);
                Return.Merge(Result);

                foreach (DataRow rk in Return.Tables["ABKOPF"].Rows)
                {
                    row = SETINTEXDB.ABPOS.NewRow();
                    row["ABPMANDANT"] = rk["ABKMANDANT"];
                    row["ABPAUFKRS"] = rk["ABKAUFKRS"];
                    row["ABPAUFLFD"] = rk["ABKAUFLFD"];
                    Result = INTEXDBC.QueryABPOS(row, 2);
                    Return.Merge(Result);

                    row = SETINTEXDB.WBBEW.NewRow();
                    row["WBEWMANDANT"] = rk["ABKMANDANT"];
                    row["WBEWAUFKRS"] = rk["ABKAUFKRS"];
                    row["WBEWAUFLFD"] = rk["ABKAUFLFD"]; 
                    Result = INTEXDBC.QueryWBBEW(row, 3);
                    Return.Merge(Result);
                    row = SETINTEXDB.ABFOLGE.NewRow();
                    row["ABFMANDANT"] = rk["ABKMANDANT"];
                    row["ABFAUFKRS"] = rk["ABKAUFKRS"];
                    row["ABFAUFLFD"] = rk["ABKAUFLFD"];
                    Result = INTEXDBC.QueryABFOLGE(row, 3);
                    Return.Merge(Result);
                }

                foreach (DataRow rp in Return.Tables["ABPOS"].Rows)
                {
                    row = SETINTEXDB.GAAUF.NewRow();
                    row["GAMANDANT"] = rp["ABPMANDANT"];
                    row["GAAUFKRS"] = rp["ABPAUFKRS"];
                    row["GAAUFLFD"] = rp["ABPAUFLFD"];
                    row["GAAUFPOS"] = rp["ABPAUFPOS"];
                    Result = INTEXDBC.QueryGAAUF(row, 2);
                    Return.Merge(Result);
                    
                    row = SETINTEXDB.ZUTEI.NewRow();
                    row["ZUMANDANT"] = rp["ABPMANDANT"];
                    row["ZUAUFKRS"] = rp["ABPAUFKRS"];
                    row["ZUAUFLFD"] = rp["ABPAUFLFD"];
                    row["ZUAUFPOS"] = rp["ABPAUFPOS"];
                    Result = INTEXDBC.QueryZUTEI(row, 2);
                    Return.Merge(Result);
                    row = SETINTEXDB.ABPR.NewRow();
                    row["ARMANDANT"] = rp["ABPMANDANT"];
                    row["ARAUFKRS"] = rp["ABPAUFKRS"];
                    row["ARAUFLFD"] = rp["ABPAUFLFD"];
                    row["ARAUFPOS"] = rp["ABPAUFPOS"];
                    Result = INTEXDBC.QueryABPR(row, 2);
                    Return.Merge(Result);
                }
                string processpartie = "";
                foreach (DataRow ry in Return.Tables["ZUTEI"].Rows)
                {
                    string partie = ry["ZUPARTIE"].ToString();
                    if(processpartie.Contains(partie+";")) continue;
                    processpartie += partie + ";";
                    row = SETINTEXDB.GPDAT.NewRow();
                    row["GPMANDANT"] = ry["ZUMANDANT"];
                    row["GPPARTIE"] = partie;
                    Result = INTEXDBC.QueryGPDAT(row, 1);
                    Return.Merge(Result); 
                }

                foreach (DataRow rp in Return.Tables["GPDAT"].Rows)
                {
                    row = SETINTEXDB.EKPOS.NewRow();
                    row["EKPMANDANT"] = rp["GPMANDANT"];
                    row["EKPAUFKRS"] = rp["GPBESTKRS"];
                    row["EKPAUFLFD"] = rp["GPBESTLFD"];
                    row["EKPAUFPOS"] = rp["GPBESTPOS"];
                    Result = INTEXDBC.QueryEKPOS(row, 1);
                    Return.Merge(Result);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ABPOS + ex.StackTrace, ex);
            }
            finally
            {
                EventDispatcher.Instance.RaiseStatus("完成已读取纱线信息", StatusEventType.StopProgress);
            }
            return Return;
        }
        public DataSet BuildEKPR(DataRow dwUI)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataTable tableLocal = null;
            try
            {
                OracleCommand or1 = or1 = OracleDB.CreateCommand();

                or1.CommandText = " select a.EKPART,a.EKPVAR,a.EKPFARBE, a.price, a.lastdat "
                    + " from last_ek_pr a ";
                setResult = Connect2Proxy(or1);
                if (setResult.Tables.Count > 0)
                {
                    tableLocal = setResult.Tables[0];
                    tableLocal.TableName = "LASTEKPR";
                }
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
    }
}
